/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.quician.webapp.ejbs;

import com.quician.fifa.Grupo;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author quician
 */

@LocalBean
@Stateless
public class GroupManager {
    
    
    @PersistenceContext //Entity Manager
    private EntityManager em;
    
    public List<Grupo> getGroups(){
    
        try{
            Query q = em.createNamedQuery("Grupo.findAll");
            return (List<Grupo>)q.getResultList();
        }
        catch(Exception e){
            Logger.getLogger(GroupManager.class.getName()).log(Level.SEVERE,"Error al extraer los grupos",e); 
            return null;
        }
    }
    
    public boolean AddGroup(Grupo grupo){
        try{
            em.persist(grupo);
            em.flush();
            return true;
        }catch(Exception e){
            Logger.getLogger(GroupManager.class.getName()).log(Level.SEVERE,"Error al guardar el nuevo grupo",e); 
            return false;
        }
    }
    
    public boolean UpdateGroup(Grupo grupo){
        try{
            em.merge(grupo);
            em.flush();
            return true;
        }catch(Exception e){
            Logger.getLogger(GroupManager.class.getName()).log(Level.SEVERE,"Error al modificar el grupo",e); 
            return false;
        }
    }
    
    public boolean DeleteGroup(Grupo grupo){
        try{
            
            Grupo groupToDelete = em.getReference(Grupo.class, grupo.getId());
            em.remove(groupToDelete);
            em.flush();
            return true;
        }catch(Exception e){
            Logger.getLogger(GroupManager.class.getName()).log(Level.SEVERE,"Error al eliminar el grupo",e); 
            return false;
        }
    }
            
    
    
}
